<template>
  <div class="app-container license">
    <div class="license-header">
      <el-button type="primary" size="mini" @click="handleRegisterCode">获取注册码</el-button>
      <el-button type="primary" size="mini" @click="dialog.importLicense = true">导入License</el-button>
    </div>
    <el-table
      v-if="list.length > 0"
      :data="list"
      style="width: 100%">
      <el-table-column
        label="License控制项名称">
        <template slot-scope="scope">
          <div class="table-link">
            <span>{{ scope.row.cnName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="值">
        <template slot-scope="scope">
          <div class="table-link">
            <span>{{ scope.row.value }}</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div v-else class="no-data">
      <p>暂无License，请<a class="no-data-click" @click="dialog.importLicense = true">导入</a>License</p>
    </div>
    <el-dialog
      :visible.sync="dialog.importLicense"
      :title="'导入License'"
      width="500px"
      @close="resetForm('importForm')">
      <el-form ref="importForm" :model="importForm" :rules="rulesImport" label-width="80px">
        <el-form-item label="序列号：" prop="seriesNumber">
          <el-input
            v-model="importForm.seriesNumber"
            :rows="5"
            type="textarea"
            placeholder="请输入序列号"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialog.importLicense = false;">取 消</el-button>
        <el-button type="primary" size="small" @click="submitForm('importForm')">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :visible.sync="dialog.registerCode"
      :title="'注册码'"
      width="500px">
      <el-input
        v-loading="listLoading"
        v-model="registerCode"
        :rows="5"
        element-loading-text="图表加载中，请稍候"
        element-loading-spinner="el-icon-loading"
        element-loading-background="#ffffff"
        type="textarea"
      />
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialog.registerCode = false;">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getLicense, importLicense, getLicenseRegistercode } from '@/api/license'

export default {
  data() {
    return {
      list: [],
      listLoading: true,
      dialog: {
        importLicense: false,
        registerCode: false
      },
      importForm: {
        seriesNumber: ''
      },
      rulesImport: {
        seriesNumber: [
          { required: true, message: '请输入序列号', trigger: 'blur' }
        ]
      },
      registerCode: ''
    }
  },
  created() {
    this._getLicense()
  },
  methods: {
    // 表单提交(importForm: 新建用户)
    submitForm() {
      this.$refs['importForm'].validate((valid) => {
        if (valid) {
          const data = {
            series_number: this.importForm.seriesNumber
          }
          this._importLicense(data)
          this.dialog.importLicense = false
          this.resetForm('importForm')
        } else {
          return false
        }
      })
    },
    // 重置表单
    resetForm(formName) {
      this.dialog.importLicense = false
      this.importForm = {
        seriesNumber: ''
      }
      this.$refs[formName].resetFields()
    },
    handleRegisterCode() {
      this.registerCode = ''
      this._getLicenseRegistercode()
      this.dialog.registerCode = true
    },
    // 获取License
    _getLicense() {
      this.listLoading = true
      getLicense().then(res => {
        if (res.message.code === 0) {
          this.listLoading = false
          this.list = res.data
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    },
    // 导入License
    _importLicense(data) {
      this.listLoading = true
      importLicense(data).then(res => {
        if (res.message.code === 0) {
          this.listLoading = false
          this.$message({ message: '导入成功', showClose: true, type: 'success' })
          this._getLicense()
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    },
    // 获取License注册码
    _getLicenseRegistercode() {
      this.listLoading = true
      getLicenseRegistercode().then(res => {
        if (res.message.code === 0) {
          this.listLoading = false
          this.registerCode = res.data
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.license {
  &-header {
    padding: 10px;
    background: #ffffff;
    font-size: 14px;
    color: #303133;
    font-weight: bold;
    .header-title {
      font-size: 14px;
      font-weight: 700;
    }
  }
  label {
    font-weight: normal
  }
  .table-action {
    color: #4285f4;
  }
  .table-link {
    &:hover {
      color: #4285f4;
    }
  }
  .no-data {
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff;
    font-size: 16px;
    opacity: .7;
    padding: 50px 0;
    .no-data-click {
      color: #077cf6;
    }
  }
}
</style>
